from django.conf import settings
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.validators import UniqueTogetherValidator
from apps.headline.models import Article
from apps.person.models import User
from apps.qa.models import Question, Label, Reply
from apps.recruit.models import Enterprise


class QuestSerializer(serializers.ModelSerializer):
    labels = serializers.StringRelatedField(many=True)
    user = serializers.StringRelatedField()
    class Meta:
        model = Question
        fields = '__all__'

class ArticleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = '__all__'

class EnterpriseSerializer(serializers.ModelSerializer):
    recruits = serializers.IntegerField(read_only=True)
    class Meta:
        model = Enterprise
        fields = '__all__'

class LabelSerializer(serializers.ModelSerializer):
    # labels = serializers.StringRelatedField(read_only=True)
    class Meta:
        model = Label
        fields = ['id','label_name']

class ReplySerializer(serializers.ModelSerializer):
    # answer_question = serializers.
    class Meta:
        model = Reply
        fields = '__all__'

class PersonSerializer(serializers.ModelSerializer):
    labels = LabelSerializer(many=True)
    questions = QuestSerializer(many=True)
    answer_question = serializers.SerializerMethodField()
    enterpises  = EnterpriseSerializer(many=True)
    collected_articles = serializers.SerializerMethodField()  # 多对多关系
    username = serializers.CharField(read_only=True)
    # password = serializers.CharField(read_only=True)
    class Meta:
        model = User
        fields = ['id', 'username', 'mobile', 'realname', 'birthday', 'sex', 'avatar', 'website', 'email',
                  'city', 'address', 'labels', 'questions', 'answer_question', 'collected_articles', 'enterpises']

        # fields = '__all__'
        extra_kwargs = {
            'username': {
                'max_length': 20,
                'min_length': 5
            },
            'password': {
                'max_length': 20,
                'min_length': 8,
                'write_only': True

            },
        }

    # 重写create方法
    def create(self, validated_data):
        # 保存用户数据并对密码加密
        user = User.objects.create_user(**validated_data)
        return user

    def get_answer_question(self, obj):
        reply = Reply.objects.filter(user=obj)
        return ReplySerializer(reply, many=True).data

    def get_collected_articles(self, obj):
        articles = Article.objects.filter(user=obj)
        return ArticleSerializer(articles, many=True).data

class UserLikeSerializer(serializers.ModelSerializer):
    """
       用户收藏
       """
    user = serializers.HiddenField(default=serializers.CurrentUserDefault())
    # validate实现唯一联合，一个商品只能收藏一次
    validators = [
        UniqueTogetherValidator(queryset=User.objects.all(), fields=('user', 'fans'), message='已经收藏')
    ]

    class Meta:
        model = User
        fields = ['user', 'fans', 'id']

class UploadSerializer(serializers.ModelSerializer):

    imgurl = serializers.CharField(read_only=True)
    class Meta:
        model = User
        fields = ['avatar','imgurl']

        extra_kwargs = {
            'avatar': {
                'max_length': 500,

            }}

class Article2Serializer(serializers.ModelSerializer):
    class Meta:
        model = Article
        fields = ['id','title']

class ArticlePersonSerializer(serializers.ModelSerializer):

    articles =  Article2Serializer(many=True,read_only=True)

    class Meta:
        model = User
        fields = ['id', 'username','avatar','fans','articles']






